<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
	public function index(){
			
		$catedata=D('Goodcategory')->tree(M('goodcategory')->where("level=0")->select());
		$goodsss=array();
		//    	var_dump($catedata);
		foreach($catedata as $v){
			$cid=$v['GoodcategoryId'];
			$goods=M('goods')->join("join goodcategory ON goodcategory.GoodcategoryId=goods.GoodcategoryId")->limit(0,4)->where("goodcategory.GoodcategoryId=$cid")->where(array('Default'=>1,'Status'=>1))->select();
			array_push($goodsss, $goods);
		}
			
		$this->assign("goodsss",$goodsss);
			
		$is_best=M('goods')->limit(0,4)->where(array('Default'=>1,'Status'=>1,'Is_best'=>1))->select();
		$this->assign("is_bests",$is_best);
		$this->display();
			
			
		//    	$model = M('goods');
		//    	//同类商品根据status判断输入，避免重复显示
		//    	$data[goods] = $model->where(array('Default'=>1))->select();
		//    	$this->assign($data);
		//        $this->display();
	}
	public function classify(){
		$model = M('goods');
		$catedata=D('Goodcategory')->tree(M('goodcategory')->where("level=0")->select());
		$goodsss=array();
		foreach($catedata as $v){
			$cid=$v['GoodcategoryId'];
			$goods=M('goods')->join("join goodcategory ON goodcategory.GoodcategoryId=goods.GoodcategoryId")->limit(0,4)->where("goodcategory.GoodcategoryId=$cid")->where(array('Default'=>1,'Status'=>1))->select();
			array_push($goodsss, $goods);
		}
			
		$this->assign("goodsss",$goodsss);
		$is_best=M('goods')->where(array('Default'=>1,'Status'=>1,'Is_best'=>1))->select();
		$this->assign("is_bests",$is_best);

		//同类商品根据status判断输入，避免重复显示
		$data[goods] = $model->where(array('Default'=>1))->select();
		//var_dump($data);
		$this->assign("catedata",$catedata);
		$this->assign($data);
		//$this->display();
		$this->display();
	}

	public function goods(){
		$gid = I('get.gid','0','int');
		//得到商品信息
		$data['goods'] = M('goods')->where(array(
		//根据id取得商品，并判断该商品是否上架(ˉ▽￣～)且没有被删除
    	  'GoodId'=>$gid,'Status'=>1))->find();
		if (empty($data['goods'])) {  //判断商品是否存在
			$this->error('该商品不存在或已经下架！');
			return ;
		}
			
    	$cid = $data['goods']['GoodcategoryId'];  //取出该商品所在的分类id

    	$data['attr'] = M('goodattribute')->where(array("GoodcategoryId"=>$cid))->select(); //取出商品属性
			
		//视图
		$data['colors']=M('goods')->distinct(true)->field('Color')->where("GoodId=$gid")->select();
		$data['versions']=M('goods')->distinct(true)->field('Version')->where("GoodId=$gid")->select();
		
		
		$where['OrderStatus']=4;
		$where['GoodId']=$gid;
		$data['ors']=M('order')->join("right join orderdetail ON orderdetail.order_OrderId=order.OrderId join goods ON orderdetail.goods_GoodId=goods.GoodId join user ON order.UserId=user.UserId")->where('orderdetail.goods_Version=goods.Version and orderdetail.goods_Color=goods.Color')->where($where)->select();
		
		$this->assign($data);
		$this->display();
	}


	public function goodsColorAjax(){
		if(IS_POST){
			//			$attrwhere['GoodcategoryId']=I('post.categoryid');		//通过分类名称来初步筛选goodatrribute表中存在的属性，尽管goods_has_goodattribute表中没有这些属性
			//这种筛选没有筛选掉商品类别一样的商品，还需要在Goods的update界面中将本商品属性和新增的goodattribute属性以外的属性给删除掉
			//			$goodattribute=M('goods_has_goodattribute')->join('right join goodattribute ON goods_has_goodattribute.goodattribute_GoodAttributeid=goodattribute.GoodAttributeid')->where($attrwhere)->select();
			$where['GoodId']=I('post.GoodId',0);
			$where['Color']=I('post.Color','');
			$version=M('goods')->field('Version')->where($where)->where("status=1")->select();
			$vv=array();
			foreach($version as $v){
				array_push($vv, $v['Version']);
			}
			$this->ajaxReturn($vv);	//返回到ajax中是msg
		}
	}


	public function goodsVersionAjax(){
		if(IS_POST){
			//			$attrwhere['GoodcategoryId']=I('post.categoryid');		//通过分类名称来初步筛选goodatrribute表中存在的属性，尽管goods_has_goodattribute表中没有这些属性
			//这种筛选没有筛选掉商品类别一样的商品，还需要在Goods的update界面中将本商品属性和新增的goodattribute属性以外的属性给删除掉
			//			$goodattribute=M('goods_has_goodattribute')->join('right join goodattribute ON goods_has_goodattribute.goodattribute_GoodAttributeid=goodattribute.GoodAttributeid')->where($attrwhere)->select();
			$where['GoodId']=I('post.GoodId',0);
			$where['Version']=I('post.Version','');
			$version=M('goods')->field('Color')->where($where)->where("status=1")->select();
			$vv=array();
			foreach($version as $v){
				array_push($vv, $v['Color']);
			}
			$this->ajaxReturn($vv);	//返回到ajax中是msg
		}
	}


	public function switgoodsAjax(){
		if(IS_POST){
			$where['GoodId']=I('post.GoodId',0);
			$where['Color']=I('post.Color','');
			$where['Version']=I('post.Version','');
			$data=M('goods')->where($where)->find();
			$this->ajaxReturn($data);	//返回到ajax中是msg
		}
	}



	public function searchGood(){
		$keyword=I("get.keyword");
		$data['GoodName'] = array('like', "%$keyword%");
		$this->assign("goods",M('goods')->where($data)->where(array('Default'=>1,'Status'=>1))->select());
		$this->display();
	}



}